/******************************************************
Filename		 : 8/4 station Control
Version	   	     : v1.0
SVN revision	 : none
Date(DD/MM/YYYY) : 20240320
Programmer	 : David Xu
Key features:
1. 修改了station的控制脚位,匹配最新的PCBA脚位说明.
2. 修改了上电检测方式,防止频繁上下电,插拔等导致检测程序出错.
3. 修改了与station号码分配相关的所有函数,增强安全性和扩展性.
4. 修复了station串口接收数据时,如果串口数据错误,或频繁被干扰,会导致指针跑飞的BUG.
5. 修复了通信数据过长时,程序会死机,或卡住的BUG.
6. 修复了会偶尔不响应pump发送的串口数据的BUG.


*********************************************************/

//***********************************************************************************************************
//  File Function: ML51 simple GPIO toggle out demo code
//***********************************************************************************************************

#include "ct_head.h"
#include "station_protocol.h"
#define UART_RX_BUFF_SIZE 256    // Define the size of the UART buffer
#define UART_TX_BUFF_SIZE 128    // Define the size of the UART buffer
int8_t UART0_RxBuff[UART_RX_BUFF_SIZE];
int8_t UART0_TxBuff[UART_TX_BUFF_SIZE];

void gpio_init(void) {
    ct_gpio_WritePin(CT_GPIO_PORT0, CT_GPIO_PIN_0 | CT_GPIO_PIN_1 | CT_GPIO_PIN_3,
                     CT_GPIO_LOW);
    ct_gpio_WritePin(CT_GPIO_PORT1, CT_GPIO_PIN_0 | CT_GPIO_PIN_1 | CT_GPIO_PIN_2 | CT_GPIO_PIN_3 | CT_GPIO_PIN_4,
                     CT_GPIO_LOW);

    ct_gpio_SetMode(CT_GPIO_PORT0, CT_GPIO_PIN_0 | CT_GPIO_PIN_1 | CT_GPIO_PIN_3,
                    CT_GPIO_MODE_OUT_PP);
    ct_gpio_SetMode(CT_GPIO_PORT1, CT_GPIO_PIN_0 | CT_GPIO_PIN_1 | CT_GPIO_PIN_2 | CT_GPIO_PIN_3 | CT_GPIO_PIN_4,
                    CT_GPIO_MODE_OUT_PP);
    ct_gpio_WritePin(CT_GPIO_PORT0, CT_GPIO_PIN_0 | CT_GPIO_PIN_1 | CT_GPIO_PIN_3,
                     CT_GPIO_LOW);
    ct_gpio_WritePin(CT_GPIO_PORT1, CT_GPIO_PIN_0 | CT_GPIO_PIN_1 | CT_GPIO_PIN_2 | CT_GPIO_PIN_3 | CT_GPIO_PIN_4,
                     CT_GPIO_LOW);
    ct_gpio_SetMode(CT_GPIO_PORT0, CT_GPIO_PIN_4,
                    CT_GPIO_MODE_INPUT);
}

void timer_init(void) {
    timer_init_t tim;

    tim.mode  = TIM_16BIT;
    tim.intt  = ct_true;
    tim.value = 65535 - 2000;    // 0xC010;
    tim.run   = ct_true;
    //
    ct_timer_inilize(TIMER0, &tim);
    //    ct_timer_inilize(TIMER2, &tim);
}

void timer_500ms(void) {
    static uint16_t LastTickTime = 0;
    uint16_t        CurrentTick  = ct_GetTick();

    if (CurrentTick - LastTickTime >= 200) {
        // P05      = !P05;
        // Anotc_WriteFrame(&g_Uart0);
        //  printf("\n Last =%d \n Current = %d \n \t     Diff = %d", LastTickTime, CurrentTick, CurrentTick - LastTickTime);
        LastTickTime = CurrentTick;
    }
}
void timer_10ms(void) {
    static uint16_t LastTickTime = 0;
    uint16_t        CurrentTick  = ct_GetTick();

    if (CurrentTick - LastTickTime >= 10) {

        //  Anotc_ReadFrame(&g_Uart0);
        LastTickTime = CurrentTick;
    }
}

void timer_1ms(void) {
    static uint16_t LastTickTime = 0;
    uint16_t        CurrentTick  = ct_GetTick();

    if (CurrentTick - LastTickTime >= 1) {
        // P05      =!P05;

        Station_MainLoop();
    }
    LastTickTime = CurrentTick;
}

void main(void) {

    gpio_init();
    ct_sys_ModifyHIRC();    // 24MHz
    ct_delay(50);
    gpio_init();
    /* Modify HIRC to 24MHz is for UART baud rate deviation not over 1% */
    ct_sys_ModifyHIRC();    // 24MHz
    timer_init();
    ct_uart_Init(&g_Uart0, CT_UART0, 115200, UART0_RxBuff, UART_RX_BUFF_SIZE, UART0_TxBuff, UART_TX_BUFF_SIZE);

    Station_Init();
    //   register_InterruptHander();
    ct_EnableAllIntt(true);
    while (1) {
       // timer_500ms();
        // timer_10ms();
        timer_1ms();
    }
}
